<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      title="关联分类"
      :visible.sync="visible"
      @closed="dialogClose"
    >
      <el-popover placement="right-end" v-model="popCatelogSelectVisible">
        <category-cascader :catelogPath.sync="catelogPath"></category-cascader>
        <div style="text-align: right; margin: 0">
          <el-button
            size="mini"
            type="text"
            @click="popCatelogSelectVisible = false"
            >取消</el-button
          >
          <el-button type="primary" size="mini" @click="addCatelogSelect"
            >确定</el-button
          >
        </div>
        <el-button slot="reference">新增关联</el-button>
      </el-popover>
      <el-table :data="cateRelationTableData" style="width: 100%">
        <el-table-column prop="id" label="#"></el-table-column>
        <el-table-column prop="brandName" label="品牌名"></el-table-column>
        <el-table-column prop="categoryName" label="分类名"></el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="deleteCateRelationHandle(scope.row.id)"
              >移除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="visible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import RelationApi from "@/api/product/catebrandrelation";
import CategoryCascader from "@/views/common/category-cascader";

export default {
  components: { CategoryCascader },
  data() {
    return {
      id: 0,
      visible: false,
      popCatelogSelectVisible: false,
      cateRelationTableData: [],
      catelogPath: [],
    };
  },
  methods: {
    init(id) {
      this.id = id;
      this.visible = true;
      RelationApi.getDataList({ brandId: id }).then(({ data }) => {
        this.cateRelationTableData = data;
      });
    },
    addCatelogSelect() {
      this.popCatelogSelectVisible = false;
      RelationApi.createRelation({
        brandId: this.id,
        categoryId: this.catelogPath[this.catelogPath.length - 1],
      }).then(({ data }) => {
        RelationApi.getDataList({ brandId: this.id }).then(({ data }) => {
          this.cateRelationTableData = data;
        });
      });
    },
    deleteCateRelationHandle(id) {
      RelationApi.deleteRelation({ id }).then(({ data }) => {
        RelationApi.getDataList({ brandId: this.id }).then(({ data }) => {
          this.cateRelationTableData = data;
        });
      });
    },
    dialogClose() {},
  },
};
</script>
<style scoped></style>
